package com.jack.thinking.java._22.swing.pane;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.*;

import com.utils.SwingConsole;

public class Menus extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] flavors = {
			"Jack", "Jane", "Li", "Zhou", "Wang", "Ly"
		};
	private JTextField t = new JTextField("No flavor", 30);
	private JMenuBar mb1 = new JMenuBar();
	private JMenu f = new JMenu("File");
	private JMenu m = new JMenu("Flavors");
	private JMenu s = new JMenu("Safety");
	
	private JCheckBoxMenuItem[] safety = {
			new JCheckBoxMenuItem("Guard"),
			new JCheckBoxMenuItem("Hide")
	};
	
	private JMenuItem[] file = {new JMenuItem("Open")};
	
	private JMenuBar mb2 = new JMenuBar();
	private JMenu fooBar = new JMenu("footBar");
	private JMenuItem[] other = {
		new JMenuItem("Foo", KeyEvent.VK_F),
		new JMenuItem("Bar", KeyEvent.VK_A),
		new JMenuItem("Baz"),
	};
	
	private JButton b = new JButton("Swap Menus");
	
	/**
	 * 按钮 交换菜单触发。
	 * @author wangbingwu
	 *
	 */
	class BL implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			JMenuBar m = getJMenuBar();
			setJMenuBar(m == mb1 ? mb2 : mb1);  //更换菜单
			validate(); //refresh the frame
		}
		
	}
	
	/**
	 *  菜单选择 时触发。
	 * @author wangbingwu
	 *
	 */
	class ML implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			JMenuItem target = (JMenuItem)e.getSource();
			String actionCommand = target.getActionCommand();
			if(actionCommand.equals("Open")) {
				String s = t.getText();
				boolean chosen = false;
				for(String flavor : flavors) {
					if(s.equals(flavor)) {
						chosen = true;
					}
				}
				if(!chosen) {
					t.setText("Choose a flavor first!");
				}else {
					t.setText("Opening " + s + ". Mmm, mm!");
				}
			}
		}
		
	}
	
	/**
	 *  菜单选择 时触发。
	 * @author wangbingwu
	 *
	 */
	class FL implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			JMenuItem target = (JMenuItem)e.getSource();
			t.setText(target.getText());
		}
		
	}
	
	class Fool implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			t.setText("Foo selected");
		}
		
	}
	
	class Barl implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			t.setText("Barl selected");
		}
		
	}
	
	class Bazl implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			t.setText("Bazl selected");
		}
		
	}
	
	class CMIL implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			JCheckBoxMenuItem target = (JCheckBoxMenuItem)e.getSource();
			// 一哥监听器可以处理多类型的按钮， 如果根据按钮上的文本判断，会面临修改按钮上的文本，导致代码失效， 所以内部代码指定按钮ID
			String actionCommand  =  target.getActionCommand();        
			if(actionCommand.equals("Guard")) {
				t.setText("Guard the Ice Cream! " + "Guarding is " + target.getState());     //是否选中。
			}else if(actionCommand.equals("Hide")) {
				t.setText("Hide the Ice Cream! " + "Is it hidden? " + target.getState());
			}
		}
		
	}
	
	public Menus() {
		ML ml = new ML();
		CMIL cmil = new CMIL();
		safety[0].setActionCommand("Guard");
		safety[0].setMnemonic(KeyEvent.VK_G);
		safety[0].addItemListener(cmil);
		safety[1].setActionCommand("Hide");
		safety[1].setMnemonic(KeyEvent.VK_H);
		safety[1].addItemListener(cmil);
		other[0].addActionListener(new Fool());
		other[1].addActionListener(new Barl());
		other[2].addActionListener(new Bazl());
		
		FL fl = new FL();
		int n = 0;
		for(String flavor : flavors) {
			JMenuItem mi = new JMenuItem(flavor);
			mi.addActionListener(fl);
			m.add(mi);
			
			if((n++ + 1) % 3 == 0) {
				m.addSeparator();
			}
		}
		
		for(JCheckBoxMenuItem sfty : safety) {
			s.add(sfty);
		}
		
		s.setMnemonic(KeyEvent.VK_A);
		f.add(s);                                //子菜单
		f.setMnemonic(KeyEvent.VK_F);
		for(int i = 0 ; i < file.length ; i++) {
			file[i].addActionListener(ml);
			f.add(file[i]);
		}
		mb1.add(f);
		mb1.add(m);
		setJMenuBar(mb1);
		t.setEditable(false);
		add(t, BorderLayout.CENTER);
		b.addActionListener(new BL());
		b.setMnemonic(KeyEvent.VK_S);
		add(b, BorderLayout.NORTH);
		for(JMenuItem oth : other) {
			fooBar.add(oth);
		}
		fooBar.setMnemonic(KeyEvent.VK_B);
		mb2.add(fooBar);
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new Menus(), 300, 200);
	}
}
